/*
Copyright (c) 2009, Dan Hagerstrand and contributors
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

* Neither the name of Cirrus Creative nor the names of its
  contributors may be used to endorse or promote products derived from this
  software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

// ******************************** //
// BlitzzEngine Example Game:       //
//     Yet Another Rolling Game     //
// ******************************** //

#include <allegro.h>
#include <Beng.h>

#define BALLSPEED 0.2
#define BALLMAXSPEED 10
#define GEMINFLUENCE 40

#define ROOMSIZEX 40
#define ROOMSIZEY 28

BengSystem bengsystem;

class Ball;

class Gem : public BengObject {

    private:

    int isFollowing_;

    public:

    static Ball *ball;
    static BengResource<BengISprite> *resource;
    static BengISample *tingsound;
    static BengISample *zweepsound;
    static BengISample *tadasound;

    Gem BENG_OBJECT_DERIVATION
    {
        isFollowing_ = 0;
    }

    virtual void onProcess(void);

};

Ball *Gem::ball;
BengResource<BengISprite> *Gem::resource;
BengISample *Gem::tingsound;
BengISample *Gem::zweepsound;
BengISample *Gem::tadasound;

class Ball : public BengObject {

    public:

    int objectCount;
    int tics;
    int bestTime;

    Ball BENG_OBJECT_DERIVATION
    {
        objectCount = tics = 0;
        bestTime = 999999;
    };

    virtual void onProcess(void);
    virtual void onPostProcess(void);
    virtual void onDraw(BengSurface *drawtarget);

};

void createjewels(BengSystem *bengsystem, BengResource<BengISprite> *gemresource)
{
    BengLayer *layer = bengsystem->scene->getLayer(1);
    short int **map;
    unsigned int sizex, sizey;

    layer->getSize(sizex, sizey);

    map = layer->getRawData();

    for (unsigned int i = 0; i < sizey; i++)
    {
        for (unsigned int j = 0; j < sizex; j++)
        {
            map[i][j] = 0;
        }
    }

    layer = bengsystem->scene->getLayer(0);

    for (int i = 0; i < 50; i++)
    {
        int x, y;

        do
        {
            x = rand()%ROOMSIZEX;
            y = rand()%ROOMSIZEY;
        } while (map[y][x] == 1);

        map[y][x] = 1;

        Gem *newgem = new Gem(layer);
        newgem->setPos(x*32 + 16, y*32 + 16);
        newgem->setSize(16, 16);
        newgem->setSprite(gemresource->getData(0));
    }
}

void Gem::onProcess(void)
{
    BengVector pos;
    BengVector ballpos;

    this->getPos(pos);
    this->ball->getPos(ballpos);

    if (!this->isFollowing_)
    {
        if (pos.x - GEMINFLUENCE < ballpos.x && pos.x + GEMINFLUENCE > ballpos.x
            && pos.y - GEMINFLUENCE < ballpos.y && pos.y + GEMINFLUENCE > ballpos.y)
        {
            this->isFollowing_ = 1;
            bengsystem.interface->iSfx->playSample(Gem::zweepsound);
        }
    }

    else
    {
        BengVector size;
        BengVector ballsize;
        BengVector speed;
        BengVector gravity;

        this->getSpeed(speed);
        speed.x = speed.x * 2 / 3;
        speed.y = speed.y * 2 / 3;

        this->setSpeed(speed.x, speed.y);

        gravity.x = (ballpos.x - pos.x)/5;
        gravity.y = (ballpos.y - pos.y)/5;
        this->setGravity(gravity.x, gravity.y);

        this->getSize(size);
        this->ball->getSize(ballsize);
        size.x += ballsize.x;
        size.y += ballsize.y;

        if (pos.x - (size.x / 2) < ballpos.x && pos.x + (size.x / 2) > ballpos.x
            && pos.y - (size.y / 2) < ballpos.y && pos.y + (size.y / 2) > ballpos.y)
        {
            destroy();
            bengsystem.interface->iSfx->playSample(Gem::tingsound);
            this->ball->objectCount++;
        }
    }

}

void Ball::onProcess(void)
{
    BengVector pos;
    BengVector speed;
    BengVector gravity;

    this->getPos(pos);
    this->getSpeed(speed);

    gravity.x = gravity.y = 0;

    if (key[KEY_LEFT])
        gravity.x = -BALLSPEED;
    else if (key[KEY_RIGHT])
        gravity.x = BALLSPEED;

    if (key[KEY_UP])
        gravity.y = -BALLSPEED;
    else if (key[KEY_DOWN])
        gravity.y = BALLSPEED;

    if (pos.x > (ROOMSIZEX*32) - 1 - 12 && speed.x > 0)
    {
        speed.x = -speed.x*5/9;
        pos.x = (ROOMSIZEX*32) - 1 - 12;
    }
    else if (pos.x < 12 && speed.x < 0)
    {
        speed.x = -speed.x*5/9;
        pos.x = 12;
    }
    if (pos.y > (ROOMSIZEY*32) - 1 - 12 && speed.y > 0)
    {
        speed.y = -speed.y*5/9;
        pos.y = (ROOMSIZEY*32) - 1 - 12;
    }
    else if (pos.y < 12 && speed.y < 0)
    {
        speed.y = -speed.y*5/9;
        pos.y = 12;
    }

    if (speed.x > BALLMAXSPEED)
        speed.x = BALLMAXSPEED;
    else if (speed.x < -BALLMAXSPEED)
        speed.x = -BALLMAXSPEED;
    if (speed.y > BALLMAXSPEED)
        speed.y = BALLMAXSPEED;
    else if (speed.y < -BALLMAXSPEED)
        speed.y = -BALLMAXSPEED;

    this->setPos(pos.x, pos.y);
    this->setSpeed(speed.x, speed.y);
    this->setGravity(gravity.x, gravity.y);

}

void Ball::onPostProcess(void)
{
    BengVector pos;
    BengVector cam;
    BengScene *scene = this->getScene();

    this->getPos(pos);
    scene->getCameraPos(cam);

    if (pos.x > cam.x + 320 + 64)
        cam.x = pos.x - 320 - 64;
    if (pos.x < cam.x + 320 - 64)
        cam.x = pos.x - 320 + 64;

    if (pos.y > cam.y + 240 + 48)
        cam.y = pos.y - 240 - 48;
    if (pos.y < cam.y + 240 - 48)
        cam.y = pos.y - 240 + 48;

    scene->setCameraPos(cam.x, cam.y);
    scene->doCameraPons();

    this->tics++;

    if (this->objectCount >= 50)
    {
        this->objectCount = 0;
        createjewels(&bengsystem, Gem::resource);

        if (this->tics < this->bestTime)
            this->bestTime = this->tics;

        this->tics = 0;

        bengsystem.interface->iSfx->playSample(Gem::tadasound);
    }
}

void Ball::onDraw(BengSurface *drawtarget)
{
    // Alleg4-specific stuff for now. In the future, drawing functions will be abstracted.
    // For now this code is kinda non-portable. Oh well.

    bengsystem.interface->iGfx->disableRegionClipping();

    rectfill(drawtarget, 0, 480 - 32, 639, 479, makecol(0, 0, 0));
    textprintf_ex(drawtarget, font, 16, 460, makecol(255, 255, 255), makecol(0, 0, 0),
        "Time: %i", this->tics);
    textprintf_ex(drawtarget, font, 160, 460, makecol(255, 255, 255), makecol(0, 0, 0),
        "Best Time: %i", this->bestTime);

}

void changelayer(BengSystem *bengsystem)
{
    BengLayer *layer;
    short int **map;
    unsigned int sizex, sizey;

    layer = bengsystem->scene->getLayer(0);

    layer->getSize(sizex, sizey);

    map = layer->getRawData();

    for (unsigned int i = 0; i < sizey; i++)
    {
        for (unsigned int j = 0; j < sizex; j++)
        {
            map[i][j] = 1;
        }
    }

}

int main(void)
{
    BengResource<BengISprite> *ballresource;
    BengResource<BengISprite> *tileresource;
    BengResource<BengISample> *tempsample;
    BengLayer *layer;
    Ball *newobj;

    bengsystem.setName("Yet Another Rolling Game");

    bengsystem.interface->init();

    bengsystem.newScene("Jewel Land");

    layer = new BengLayer(ROOMSIZEX, ROOMSIZEY);
    layer->setTileSize(32, 32);
    bengsystem.scene->addLayer(layer);
    layer = new BengLayer(ROOMSIZEX, ROOMSIZEY);
    layer->setTileSize(32, 32);
    bengsystem.scene->addLayer(layer);
    changelayer(&bengsystem);
    layer = bengsystem.scene->getLayer(0);

    // This code is stupid. :)
    tileresource = bengsystem.spriteResourceList->create("data/Tile1.bmp");
    bengsystem.spriteResourceList->append("data/Tile2.bmp", tileresource);
    bengsystem.spriteResourceList->append("data/Tile3.bmp", tileresource);
    bengsystem.spriteResourceList->append("data/Tile4.bmp", tileresource);
    //bengsystem.appendSpriteResource("data/Tile5.bmp", tileresource);
    //bengsystem.appendSpriteResource("data/Tile6.bmp", tileresource);
    //bengsystem.appendSpriteResource("data/Tile7.bmp", tileresource);

    layer->setTileset(tileresource);

    ballresource = bengsystem.spriteResourceList->create("data/Ball.bmp");
    Gem::resource = bengsystem.spriteResourceList->create("data/Gem.bmp");

    tempsample = bengsystem.sampleResourceList->create("data/Ting.wav");
    Gem::tingsound = tempsample->getData(0);
    Gem::zweepsound = bengsystem.sampleResourceList->append("data/Zweep.wav", tempsample);
    Gem::tadasound = bengsystem.sampleResourceList->append("data/Tada.wav", tempsample);

    newobj = new Ball(layer);
    newobj->setPos(16, 16);
    newobj->setSize(24, 24);

    newobj->setSprite(ballresource->getData(0));

    layer = bengsystem.scene->getLayer(1);

    layer->setVisibility(false);

    createjewels(&bengsystem, Gem::resource);

    Gem::ball = newobj;

    bengsystem.scene->setCameraRegion(0, 0, 640, 448);

    bengsystem.mainLoop();

    return 0;
}

BENG_END_OF_MAIN

